package com.zut.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张晨
 * @date 2024/9/6 16:48
 * @desc
 */
@WebFilter("/*")
public class EncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        req.setCharacterEncoding("UTF-8");

        resp.setContentType("application/json;charset=UTF-8");
        //允许所有的跨域
        resp.setHeader("Access-Control-Allow-Origin", "*");
        //允许的请求方式
        resp.setHeader("Access-Control-Allow-Methods", "*");
        //允许所有的Headers
        resp.setHeader("Access-Control-Allow-Headers", "*");

        filterChain.doFilter(req,resp);
    }
}
